package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.MyFeedBackBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 16/1/26.
 */
public class FeedBackAdapter extends MyAdapter<MyFeedBackBean.DataEntity> {

    String icon;
    ViewHolder holder;
    refresh refresh;


    public FeedBackAdapter(Context context, List<MyFeedBackBean.DataEntity> list) {
        super(context, list);
        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(context);
        icon = user.getUthumb();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_feedback, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        loadData(holder, position);

        return convertView;
    }

    private void loadData(final ViewHolder holder, int position) {
        if (TextUtils.isEmpty(list.get(position).getResponse())) {

            holder.feedbackClient.setVisibility(View.GONE);
        } else {
            holder.feedbackClient.postDelayed(new Runnable() {
                @Override
                public void run() {
                    holder.feedbackClient.setVisibility(View.VISIBLE);
                    refresh.refresh();
                }
            }, 1000);
        }

        holder.feedbackMineContent.setText(list.get(position).getQuestion());
        holder.feedbackClientContent.setText(list.get(position).getResponse());

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(context);
        String myIcon = dataEntity.getUthumb();
        Glide
                .with(context)
                .load(myIcon)
                .placeholder(R.mipmap.login_head)
                .transform(new GlideCircle(context))
                .into(holder.feedbackMineIcon);

    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_feedback.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.feedback_mine_content)
        TextView feedbackMineContent;
        @Bind(R.id.feedback_mine_icon)
        ImageView feedbackMineIcon;
        @Bind(R.id.feedback_mine)
        RelativeLayout feedbackMine;
        @Bind(R.id.feedback_client_icon)
        ImageView feedbackClientIcon;
        @Bind(R.id.feedback_client_content)
        TextView feedbackClientContent;
        @Bind(R.id.feedback_client)
        LinearLayout feedbackClient;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface refresh {
        void refresh();
    }

    public void setRefresh(FeedBackAdapter.refresh refresh) {
        this.refresh = refresh;
    }
}
